package com.tms.springboot.result;

import lombok.Data;

import java.io.Serializable;

/**
 * @Author: hui
 * @Description: 统一返回实体
 */
@Data
public class JsonResult implements Serializable {
    private Boolean flag;
    private Integer code;
    private Object data;
    private String message;

    public JsonResult() {
    }

    // 成功或者失败都能走这个
    public JsonResult(boolean flag) {
        this.flag = flag;
        this.message = flag ? ResultCode.SUCCESS.getMessage() : ResultCode.COMMON_FAIL.getMessage();
        this.code = flag ? ResultCode.SUCCESS.getCode() : ResultCode.COMMON_FAIL.getCode();
    }

    // 成功或者失败都能走这个，并且可以传一个枚举来改变默认枚举的值
    public JsonResult(boolean flag, ResultCode resultEnum) {
        this.flag = flag;
        // 传来的枚举为null就用默认的，不为null就用传来的枚举
        this.code = flag ? (resultEnum == null ? ResultCode.SUCCESS.getCode() : resultEnum.getCode()) : (resultEnum == null ? ResultCode.COMMON_FAIL.getCode() : resultEnum.getCode());
        this.message = flag ? (resultEnum == null ? ResultCode.SUCCESS.getMessage() : resultEnum.getMessage()) : (resultEnum == null ? ResultCode.COMMON_FAIL.getMessage() : resultEnum.getMessage());
    }

    // 成功或者失败都能用
    // 用户可以传一个任意对象过来，用默认的成功或者失败的枚举
    public JsonResult(boolean flag, Object data) {
        this.flag = flag;
        this.code = flag ? ResultCode.SUCCESS.getCode() : ResultCode.COMMON_FAIL.getCode();
        this.message = flag ? ResultCode.SUCCESS.getMessage() : ResultCode.COMMON_FAIL.getMessage();
        this.data = data;
    }

    // 成功或者失败都能用
    // 用户可以传一个任意对象和自定义枚举过来
    public JsonResult(boolean flag, ResultCode resultEnum, Object data) {
        this.flag = flag;
        this.code = flag ? (resultEnum == null ? ResultCode.SUCCESS.getCode() : resultEnum.getCode()) : (resultEnum == null ? ResultCode.COMMON_FAIL.getCode() : resultEnum.getCode());
        this.data = data;
        this.message = flag ? (resultEnum == null ? ResultCode.SUCCESS.getMessage() : resultEnum.getMessage()) : (resultEnum == null ? ResultCode.COMMON_FAIL.getMessage() : resultEnum.getMessage());
    }
}